﻿using DosTexPrj.Core.Models;
using DosTexPrj.Core.RepositoriesInterface;
using NUnit.Framework;

namespace DosTexPrj.Implement.NHibernate.Test.Repositories
{
    [TestFixture]
    public class TestDyeOrder : RepositoryFixtureBase
    {
        private IRepository _repository;
        private DeliveryMethod _deliveryMethod;
        private PaymentMethod _paymentMethod;
        private Composition _composition;
        private OrderStatus _orderStatus;
        private FabricType _fabricType;

        protected override void Context()
        {
            this._repository = MyServiceLocator.DoGetInstance<IRepository>();
            this._deliveryMethod = this._repository.FindByKey<DeliveryMethod>(1);
            this._paymentMethod = this._repository.FindByKey<PaymentMethod>(1);
            this._composition = this._repository.FindByKey<Composition>(1);
            this._orderStatus = this._repository.FindByKey<OrderStatus>(1);
            this._fabricType = this._repository.FindByKey<FabricType>(1);
        }

        [Test]
        public void Can_Create_DyeOrder_With_Details()
        {
            var dyeOrder = this.SaveDyeOrder();
            Assert.IsTrue(dyeOrder != null && dyeOrder.Id > 0
                        && dyeOrder.Details != null && dyeOrder.Details.Count > 0);
        }

        [Test]
        public void Can_Add_More_Detail()
        {
            var dyeOrder = this.SaveDyeOrder();
            var dyeOrderDetail = new DyeOrderDetail
            {
                BelongToDyeOrder = dyeOrder,
                Length = 200,
                Weight = 200
            };
            dyeOrderDetail = this._repository.Save<DyeOrderDetail>(dyeOrderDetail);
            dyeOrder = this._repository.FindByKey<DyeOrder>(dyeOrder.Id);
            Assert.IsTrue(dyeOrder.Details.Count == 4);
        }

        [Test]
        public void Can_Delete_One_Detail()
        {
            var dyeOrder = this.SaveDyeOrder();
            dyeOrder.Details.RemoveAt(0);
            dyeOrder = this._repository.Save<DyeOrder>(dyeOrder);
            Assert.IsTrue(dyeOrder.Details.Count == 2);
        }

        [Test]
        public void Can_Delete_All_Detail()
        {
            var dyeOrder = this.SaveDyeOrder();
            this._repository.Delete<DyeOrderDetail>(x => x.BelongToDyeOrder == dyeOrder);
            dyeOrder = this._repository.FindByKey<DyeOrder>(dyeOrder.Id);
            Assert.IsTrue(dyeOrder.Details == null || dyeOrder.Details.Count == 0);
        }

        #region --- private methods ---

        private Order SaveOrder()
        {
            var customer = this.CreateDummyCustomer("Test DyeOrder");
            customer = this._repository.Save<Customer>(customer);
            var order = this.CreateDummyOrder(customer, _paymentMethod, _deliveryMethod, _orderStatus);
            order = this._repository.Save<Order>(order);
            return order;
        }

        private Supplier SaveSupplier()
        {
            var supplier = this.CreateDummySupplier("Test DyeOrder");
            supplier = this._repository.Save<Supplier>(supplier);
            return supplier;
        }

        private DyeOrder SaveDyeOrder()
        {
            var order = this.SaveOrder();
            var supplier = this.SaveSupplier();
            var dyeOrder = this.CreateDummyDyeOrder(order, supplier, _composition, _orderStatus);
            var dyeOrderDetails = this.CreateDummyDyeOrderDetails(dyeOrder);
            dyeOrder.Details = dyeOrderDetails;
            dyeOrder = this._repository.Save<DyeOrder>(dyeOrder);
            return dyeOrder;
        }

        #endregion --- private methods ---
    }
}